<template>
  <div class="login_container">
    <!-- <h1>飞凡空间后台管理系统</h1> -->
    <div class="login_title">
      <h1>飞凡空间</h1>
    </div>
    <div class="login_box">
      <div class="avatar_box">
        <img src="@/assets/logo.png"/>
      </div>
       <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginFormRules"
          label-width="60px"
          class="login_form"
        >
          <el-form-item label="账号" prop="username">
            <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              prefix-icon="iconfont icon-3702mima"
            ></el-input>
          </el-form-item>
          <el-form-item class="btns">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="reset">重置</el-button>
          </el-form-item>
       </el-form>
      </div>
  </div>
</template>

<script>
import Qs from 'qs'

export default {
  data(){
    return {
      loginForm: {
        username: 'admin',
        password: '000000'
      },
      // 表单验证
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
      reset(){
        this.$refs.loginFormRef.resetFields();
        this.loginForm.username = ''
        this.loginForm.password = ''
      },
      login() {
        var v1 = this 
        this.$refs.loginFormRef.validate(async valid => {
            if (!valid) return
            this.axios({
              method: "post",
              url: "login",
              transformRequest: [function (data) {
                 return Qs.stringify(data)
              }],
              data: v1.loginForm
            }).then(function(res){
               if (res.status === 200){
                 window.sessionStorage.setItem("token", res.data.data.token);
                 v1.$message.success(res.data.msg);
                 v1.$router.push("/home")
               }else{
                 v1.$message.error(res.data.msg);
               }
            })
        })
      }
    }
}
</script>

<style lang="less" scoped>
.login_container{
  background-color: #2b4b6b;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login_title{
  position: absolute;
  top: 5%;
  color: white;
}
.login_box{
  position: relative;
  height: 300px;
  width: 450px;
  background-color: white;
  border-radius: 3px;


}
.avatar_box{
  width: 130px;
  height:130px;
  border: 1px solid #eee;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  position: relative;
  left: 160px;
  bottom: 50px;
  img{
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}
.login_form {
  padding: 0 15px;
  position: relative;
  bottom: 20px;
  box-sizing: border-box;
}
.btns {
  margin-left: 50%;
}

</style>
